/**
 ****************************************************************************************************
 * @file        main.c
 * @author      
 * @version     V1.0
 * @date        2020-04-20
 * @brief       IMU901 & BlueTooth test
 * @license     Copyright (c) 2020-2032, HDWX team
 ****************************************************************************************************
 TODO list
 1.data flow type
  - original data flow (transparent transmission)
  - timeStamp with orignal data flow
  - timeStamp with new data flow, include pose etc. by algorithm lib
 2.change usart index with pins followed by PCB
 3.data transfer by usart pin/ BlueTooth by pin level
 4.change the comment to English
 ****************************************************************************************************
 */

#include "./stm32f4xx_it.h"
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"

#include "./BSP/LED/led.h"
#include "./BSP/USART2/usart2.h"
#include "./BSP/USART6/usart6.h"

#include "./IMU901/imu901.h"

#include "./XLOG/xlog.h"

int main(void)
{
    uint32_t times = 0;
    uint8_t ch;
	
	HAL_Init();
    sys_stm32_clock_init();     			/* 设置时钟, 180Mhz */
    delay_init(168);                         /* 延时初始化 */
    usart_init(115200);                     /* 串口初始化为115200 */
    led_init();                             /* 初始化LED */
    usart2_init(115200);
    imu901_init();							/* IMU901模块初始 */
	
	XLOG_INIT();
	XLOG_CLEAR();
	
	
	usart6_init(115200);
	
	uint32_t k = 0;

    while (1)
    {	
		#if 0 /* bt test */
		uint8_t charactor = 0;
		uint16_t usart6_buffLen = usart6_getRxData(&charactor, 1);
		if(usart6_buffLen > 0)
		{
			printf("BT data: 0x%x\r\n", charactor);
			charactor++;
			usart6_sendData(&charactor, 1);
		}
		#endif
		
        if (imu901_uart_receive(&ch, 1)) 	/*!< 获取串口fifo一个字节 */
        {
            if (imu901_unpack(ch)) 			/*!< 解析出有效数据包 */
            {
                if (rxPacket.startByte2 == UP_BYTE2) 			/*!< 主动上传的数据包 */
                {
                    atkpParsing(&rxPacket);
                }
            }
        }
        else
        {
            delay_ms(1);

            times++;

            if (times % 300  == 0) LED0_TOGGLE(); 	/* 闪烁LED,提示系统正在运行 */

            if (times % 1000 == 0) 					/*!< 1秒打印一次数据 */
            {
				#if 0
                printf("\r\n");
                printf("姿态角[XYZ]:    %-6.1f     %-6.1f     %-6.1f   (°)\r\n", attitude.roll, attitude.pitch, attitude.yaw);
                printf("加速度[XYZ]:    %-6.3f     %-6.3f     %-6.3f   (g)\r\n", gyroAccData.faccG[0], gyroAccData.faccG[1], gyroAccData.faccG[2]);
                printf("角速度[XYZ]:    %-6.1f     %-6.1f     %-6.1f   (°/s)\r\n", gyroAccData.fgyroD[0], gyroAccData.fgyroD[1], gyroAccData.fgyroD[2]);
                printf("磁场[XYZ]  :    %-6d     %-6d     %-6d   (uT)\r\n", magData.mag[0], magData.mag[1], magData.mag[2]);
                printf("气压 	   :    %-6dPa   %-6dcm\r\n", baroData.pressure, baroData.altitude);
				#elif 0
				printf("IMU\r\n");
				printf("姿态角[XYZ]:    %-6.1f     %-6.1f     %-6.1f   (°)\r\n", attitude.roll, attitude.pitch, attitude.yaw);
				
				#elif 0
				
				LOGI("姿态角[XYZ]:    %f     %f     %f   (°)", attitude.roll, attitude.pitch, attitude.yaw);
                LOGI("加速度[XYZ]:    %f     %f     %f   (g)", gyroAccData.faccG[0], gyroAccData.faccG[1], gyroAccData.faccG[2]);
                LOGI("角速度[XYZ]:    %f     %f     %f   (°/s)", gyroAccData.fgyroD[0], gyroAccData.fgyroD[1], gyroAccData.fgyroD[2]);
                LOGI("磁场[XYZ]  :    %d     %d     %d   (uT)", magData.mag[0], magData.mag[1], magData.mag[2]);
                LOGI("气压 	     :    %dPa   %dcm", baroData.pressure, baroData.altitude);
				LOGI("");
				
				#else
				
			    #endif
            }
			
			#if 1
			
			LOGI("[XYZ]:    %f     %f     %f   (°)", attitude.roll, attitude.pitch, attitude.yaw);
			LOGD("加速度[XYZ]:    %f     %f     %f   (g)", gyroAccData.faccG[0], gyroAccData.faccG[1], gyroAccData.faccG[2]);
			LOGT("角速度[XYZ]:    %f     %f     %f   (°/s)", gyroAccData.fgyroD[0], gyroAccData.fgyroD[1], gyroAccData.fgyroD[2]);
			LOGW("磁场[XYZ]  :    %d     %d     %d   (uT)", magData.mag[0], magData.mag[1], magData.mag[2]);
			LOGE("气压 	     :    %dPa   %dcm", baroData.pressure, baroData.altitude);
			LOGI("");
			
			#endif
        }
    }
}



